package com.service.user.impl;

import com.dao.UserDao;
import com.dao.UserEnterDao;
import com.service.user.UserEnterService;
import com.utils.SqlSessionGetter;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.UUID;

/** 提供用户登录、注册、修改密码相关功能 */
@Service
public class UserEnterServiceImpl implements UserEnterService {
  /**
   * 手机号是否注册过
   *
   * @param phoneNumber 手机号
   * @return 注册过返回1，没注册返回0
   */
  @Override
  public int isPhoneRegistered(String phoneNumber) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    UserEnterDao dao = session.getMapper(UserEnterDao.class);
    int ret = dao.selectPhoneCount(phoneNumber);
    session.close();
    return ret;
  }

  /**
   * 用户名是否注册过
   *
   * @param userName 用户名
   * @return 注册过返回1，没注册返回0
   */
  @Override
  public int isNameRegistered(String userName) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    UserEnterDao dao = session.getMapper(UserEnterDao.class);
    int ret = dao.selectNameCount(userName);
    session.close();
    return ret;
  }

  /**
   * 注册，同时在user表中和user_login表中添加数据
   *
   * @param phoneNumber 手机号
   * @param userName 用户名
   * @param pswd 密码
   * @return 注册成功返回用户编号，失败返回0
   */
  @Override
  public String register(String phoneNumber, String userName, String pswd) {
    // 生成随机的用户编号
    String userId = UUID.randomUUID().toString().replace("-", "");
    SqlSession session = SqlSessionGetter.getSqlSession();
    UserEnterDao enterDao = session.getMapper(UserEnterDao.class);
    enterDao.insertUserEnter(userId, phoneNumber, userName, pswd);
    session.commit();

    UserDao dao = session.getMapper(UserDao.class);
    dao.insertAUser(userId, userName);
    session.commit();

    session.close();
    return userId;
  }

  /**
   * 用户直接根据手机号登录
   *
   * @param phoneNumber 手机号
   * @return 登陆成功返回用户编号，失败返回null
   */
  @Override
  public String directlyLogin(String phoneNumber) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    UserEnterDao enterDao = session.getMapper(UserEnterDao.class);
    String userId = enterDao.selectIdByPhoneNumber(phoneNumber);
    session.close();
    return userId;
  }

  /**
   * 用户根据手机号/用户名、密码登录
   *
   * @param account 手机号或用户名
   * @return 登陆成功返回用户编号，失败返回null
   */
  @Override
  public String tryLogin(String account, String pswd) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    UserEnterDao dao = session.getMapper(UserEnterDao.class);
    String userId = dao.selectIdByAccountPswd(account, pswd);
    session.close();
    return userId;
  }

  /**
   * 重置用户密码
   *
   * @param phoneNumber 手机号
   * @param newPswd 新密码
   * @return 重置成功返回1，否则返回0
   */
  @Override
  public int resetPswd(String phoneNumber, String newPswd) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    UserEnterDao dao = session.getMapper(UserEnterDao.class);
    int row = dao.updatePswd(phoneNumber, newPswd);
    session.commit();
    session.close();
    return row;
  }

  /**
   * 修改手机号
   *
   * @param userId 用户编号
   * @param newPhoneNumber 新的手机号
   * @return 修改成功返回1，失败返0
   */
  @Override
  public int changePhoneNumber(String userId, String newPhoneNumber) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    UserEnterDao dao = session.getMapper(UserEnterDao.class);
    int row = dao.updatePhoneNumber(userId, newPhoneNumber);
    session.commit();
    session.close();
    return row;
  }
}
